/*
 *  main.c
 *
 *  This code contains the main function of the program.
 *
 *  Copyright (C) 2010 LIMSI-CNRS (Orsay-France) and EFREI (Paris-France)
 *
 *  Authors: Rami Ajaj, Nicolas Flasque and Frederic Vernier.
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU Lesser General Public License as
 *  published by the Free Software Foundation; either version 2.1 of the
 *  License, or (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Lesser General Public License for more details.
 *
 *  $Id$
 */

#include "TUIO_receiver.h"
#include "MTouchware_handlers.h"
#include "MTouchware_senders.h"
#include "MTouchware_control_receiver.h"
#include "MTouchware_touch.h"
void usage();

/**
 *  DEFAULT_TUIO_IN_PORT defines the UDP port that the TUIO_receiver will listen to by default
 */
#define DEFAULT_TUIO_IN_PORT     "3333"
#define DEFAULT_TUIO_OUT_PORT    "7777"
#define DEFAULT_CONTROL_PORT     "1234"
#define DEFAULT_TUIO_OUT_ADDRESS "127.0.0.1"

int main(int argc, char *argv[])
{
	char control_port[8];
	char output_port[8];
	char input_port[8];
    char output_ipaddress[256];

	// pthread needed for the MTouchware control receiver
	pthread_t control_receiver_thread;
	int thread_id;

    // application data

    t_applicationData appData;
    initAppData(&appData);

    strcpy(input_port,       DEFAULT_TUIO_IN_PORT);
    strcpy(output_port,      DEFAULT_TUIO_OUT_PORT);
    strcpy(control_port,     DEFAULT_CONTROL_PORT);
    strcpy(output_ipaddress, DEFAULT_TUIO_OUT_ADDRESS);

	printf("MTouchWare level 0: system launched ! \n");
    fflush(stdout);

	// if one argument then no control receiver and only output port is specified
	if (argc>5 || (argc==2 && strcmp(argv[1],"--help")))
	{
        usage();
    }
    else if (argc == 2)
    {
		strcpy(input_port,       argv[1]);
	}
	else if (argc==3)
	{
		strcpy(input_port,       argv[1]);
		strcpy(output_ipaddress, argv[2]);
    }
    else if (argc==4)
    {
		strcpy(input_port,       argv[1]);
		strcpy(output_ipaddress, argv[2]);
		strcpy(output_port,      argv[3]);
    }
    else if (argc==5)
    {
		strcpy(input_port,       argv[1]);
		strcpy(output_ipaddress, argv[2]);
		strcpy(output_port,      argv[3]);
		strcpy(control_port,     argv[4]);
    }

    thread_id = pthread_create(&control_receiver_thread, NULL, MTouchware_control_receiver , (void*)control_port); //creating the MTouchware_control_receiver

	if (thread_id<0)
	{
	  printf("MTouchWare level 2 is not working. MTouchWare cannot be controlled from outside\n");
    }

	printf("MTouchWare level 1 : OSC messages on port %s are filtered and resend on port %s\n", input_port, output_port);

	create_MTouchware_senders(output_ipaddress, output_port);  //creating the sender
	setAppLoSender(&appData, MTouchware_senders[0]);

	TUIO_receiver(input_port,&appData); // creating TUIO_receiver

	delete_MTouchware_senders();

	return 0;
}

void usage()
{
  printf("usage : mtouchware [inputport] [outputaddress] [outputport] [controlport]\n");
  return;
}
